import { Redirect } from 'react-router';
import { getLocal } from '../util/localset';
import { connect } from 'umi';

//重新获取菜单
import { navBarlist } from '../api/index';
//登录鉴权
const warLogin = (props) => {
  const { dispatch } = props;
  const isLogin = getLocal('userid') || null;

  if (isLogin) {
    // console.log(props.navbarlist.Navbarlist, "warp");
    //没有菜单就继续发送  获取
    if (props.navbarlist.Navbarlist.length === 0) {
      console.log(11111);
      const getMenuData = async () => {
        const { data } = await navBarlist({ parentId: 1001 });
        console.log(data, 'menudata');
        dispatch({
          type: 'navbarlist/setNavbar',
          payload: data,
        });
      };
      getMenuData();
    }
    /* 
     let roles = JSON.parse(localStorage.getItem('roles'));
      roles = typeof roles === "string" ? [roles]: roles;
      const isAuthorized = roles.indexOf("ROLE_ADMIN") !== -1; */
    /* if (isAuthorized) {
      return <div>{ props.children }</div>;
    } */
    return <div>{props.children}</div>;
  }
  return <Redirect to="/login" />;
};
export default connect(({ navbarlist }) => ({ navbarlist }))(warLogin);
